package com.ruoyi.ly.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.ly.domain.MobileBanner;
import com.ruoyi.ly.domain.dto.MobileBannerAddDTO;
import com.ruoyi.ly.domain.dto.MobileBannerUpdateDTO;
import com.ruoyi.ly.domain.dto.PageMobileBannerDTO;
import com.ruoyi.ly.domain.vo.MobileBannerVO;
import com.ruoyi.ly.domain.vo.PageMobileBannerVO;
import com.ruoyi.ly.mapper.MobileBannerMapper;
import com.ruoyi.ly.service.MobileBannerService;
import com.ruoyi.ly.utils.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MobileBannerServiceImpl implements MobileBannerService {

    @Autowired
    private MobileBannerMapper mobileBannerMapper;

    @Override
    public void update(MobileBannerUpdateDTO mobileBannerUpdateDto) {
        MobileBanner mobileBanner =new MobileBanner();
        BeanUtils.copyProperties(mobileBannerUpdateDto,mobileBanner);
        mobileBannerMapper.update(mobileBanner);
    }

    @Override
    public void save(MobileBannerAddDTO mobileBannerAddDTO) {
        MobileBanner mobileBanner =new MobileBanner();
        BeanUtils.copyProperties(mobileBannerAddDTO,mobileBanner);
        mobileBanner.setCreateTime(String.valueOf(System.currentTimeMillis()));
        mobileBannerMapper.insert(mobileBanner);
    }

    @Override
    public PageResult pageQuery(PageMobileBannerDTO pageMobileBannerDTO) {
        PageHelper.startPage(pageMobileBannerDTO.getPage(), pageMobileBannerDTO.getPageSize());
        Page<PageMobileBannerVO> page = mobileBannerMapper.pageQuery(pageMobileBannerDTO);
        long total = page.getTotal();
        List<PageMobileBannerVO> records = page.getResult();
        return new PageResult(total,records);
    }

    @Override
    public void delete(Long id) {
        mobileBannerMapper.delete(id);
    }

    @Override
    public MobileBannerVO selectById(Long id) {
        MobileBannerVO mobileBannerVO = mobileBannerMapper.selectById(id);
        return mobileBannerVO;
    }
}
